<?php
namespace app\serverfund\logic;

use \app\manage\logic\BaseLogic;
use \think\Db;
/**
 * @author:      李颖
 * @DateTime:    2017-12-15 16:05:56
 * @Description: 有关订单的操作
 */
class Order extends BaseLogic
{
    protected $config = 'db_config_fund';
    protected $name = 'fund_account';
   
    /*我的订单 - 去支付*/
    public function payOrderTransactionOperate($data): array 
    {
        $userInfo = json_encode($data['user_info']);
        $oppositeInfo = json_encode($data['opposite_info']);
        //开启事务
        $this->db()->startTrans();
        try{
            if($data['integral_deduction']){
                //直接扣除用户积分个数->$data['integral_deduction']
                $transferIntegral['user_id'] = $data['user_id'];
                $transferIntegral['opposite_id'] = $data['opposite_id'];
                $transferIntegral['integral'] = $data['integral_deduction'];
                $transferIntegral['update_ip'] = $data['update_ip'];
                $transferIntegral['app_id'] = $data['app_id'];
                $transferIntegral['user_info'] = $userInfo;
                $transferIntegral['opposite_info'] = $oppositeInfo;
                $transferIntegral['remark'] = '支付订单使用积分抵扣';
                $transferIntegralRes = logic('\app\serverfund\logic\IntegralAccount')->transfer($transferIntegral);
                if($transferIntegralRes['status'] != SUCCESS_STATUS){
                    $this->db()->rollback();
                    return $transferIntegralRes;
                }

                if($data['pay_method'] == 'currency'){
                    //给用户充值艺货币->$data['integral_deduction_currency']
                    $rechargeCurrency['user_id'] = $data['user_id'];
                    $rechargeCurrency['currency'] = $data['integral_deduction_currency'];
                    $rechargeCurrency['update_ip'] = $data['update_ip'];
                    $rechargeCurrency['app_id'] = $data['app_id'];
                    $rechargeCurrency['user_info'] = $userInfo;
                    $rechargeCurrency['opposite_info'] = $oppositeInfo;
                    $rechargeCurrency['remark'] = '支付订单使用积分抵扣,积分转为艺货币';
                    $rechargeCurrencyRes = logic('\app\serverfund\logic\CurrencyAccount')->prepaid($rechargeCurrency);
                    if($rechargeCurrencyRes['status'] != SUCCESS_STATUS){
                        $this->db()->rollback();
                        return $rechargeCurrencyRes;
                    }
                }else{
                    //给用户充值人民币->$data['integral_deduction_money']
                    $rechargeFund['user_id'] = $data['user_id'];
                    $rechargeFund['money'] = $data['integral_deduction_money'];
                    $rechargeFund['update_ip'] = $data['update_ip'];
                    $rechargeFund['app_id'] = $data['app_id'];
                    $rechargeFund['user_info'] = $userInfo;
                    $rechargeFund['opposite_info'] = $oppositeInfo;
                    $rechargeFund['remark'] = '支付订单使用积分抵扣,积分转为人民币';
                    $rechargeFundRes = logic('\app\serverfund\logic\FundAccount')->prepaid($rechargeFund);
                    if($rechargeFundRes['status'] != SUCCESS_STATUS){
                        $this->db()->rollback();
                        return $rechargeFundRes;
                    }
                }
            }
            //根据商品A的最终价格和商品A的个数进行冻结用户人民币/艺货币的值“最终价格”  冻结次数：商品A的购买个数
            //根据商品B的最终价格和商品B的个数进行冻结用户人民币/艺货币的值“最终价格”  冻结次数：商品B的购买个数
            $freezeCounts = 1;   //用于冻结次数
            foreach ($data['order_goods_info'] as $k => $v) {
                for($i = 1; $i <= $v['order_number']; $i++){
                    if($data['pay_method'] == 'currency'){
                        //冻结用户数字币个数 $v['final_currency_price'] $i 次
                        $freezeCurrency['user_id'] = $data['user_id'];
                        $freezeCurrency['opposite_id'] = $data['opposite_id'];
                        $freezeCurrency['currency'] = $v['final_currency_price'];
                        $freezeCurrency['update_ip'] = $data['update_ip'];
                        $freezeCurrency['app_id'] = $data['app_id'];
                        $freezeCurrency['user_info'] = $userInfo;
                        $freezeCurrency['opposite_info'] = $oppositeInfo;
                        $freezeCurrency['type'] = 'order';
                        $freezeCurrency['obj_id'] = $data['id'];
                        $freezeCurrency['freeze_counts'] = $freezeCounts;
                        $freezeCurrency['remark'] = '数字币支付订单，冻结数字币';
                        $freezeCurrencyRes = logic('\app\serverfund\logic\CurrencyAccount')->freeze($freezeCurrency);
                        if($freezeCurrencyRes['status'] != SUCCESS_STATUS){
                            $this->db()->rollback();
                            return $freezeCurrencyRes;
                        }
                    }else{
                        //冻结用户人民币 $v['final_price'] $i 次
                        $freezeFund['user_id'] = $data['user_id'];
                        $freezeFund['opposite_id'] = $data['opposite_id'];
                        $freezeFund['money'] = $v['final_price'];
                        $freezeFund['update_ip'] = $data['update_ip'];
                        $freezeFund['app_id'] = $data['app_id'];
                        $freezeFund['user_info'] = $userInfo;
                        $freezeFund['opposite_info'] = $oppositeInfo;
                        $freezeFund['type'] = 'order';
                        $freezeFund['obj_id'] = $data['id'];
                        $freezeFund['freeze_counts'] = $freezeCounts;
                        $freezeFund['remark'] = '人民币支付订单，冻结人民币';
                        $freezeFundRes = logic('\app\serverfund\logic\FundAccount')->freeze($freezeFund);
                        if($freezeFundRes['status'] != SUCCESS_STATUS){
                            $this->db()->rollback();
                            return $freezeFundRes;
                        }
                    }
                    $freezeCounts++;
                }
            }
            // 提交事务
            $this->db()->commit();
        } catch (\Exception $e) {
            // 回滚事务
            $this->db()->rollback();
            return server_err(null, '数据库异常:' . $e->getMessage());
        }
        return auto_return($data, '成功支付订单');
    }


}